﻿namespace BM.Tools.WebControls.MForm.Controls
{
    using System.Collections.ObjectModel;
    using System.Linq;
    using System.Xml.Schema;

    /// <summary>
    /// A class that contains a collection of <see cref="DateTimeFormatting" /> objects, where the data type of these formattings should be distinct.
    /// </summary>
    public class DateTimeFormattingCollection : KeyedCollection<XmlTypeCode, DateTimeFormatting>
    {
        /// <summary>
        /// Initializes a new instance of the <see cref="DateTimeFormattingCollection"/> class.
        /// </summary>
        public DateTimeFormattingCollection()
        {
            CreateDefaultCollection();
        }

        /// <summary>
        /// Inserts an element into the <see cref="T:System.Collections.ObjectModel.KeyedCollection`2"/> at the specified index, or if a formatting with the data type already exists, overwrites the old formatting with the new one.
        /// </summary>
        /// <param name="index">
        /// The zero-based index at which item should be inserted.
        /// </param>
        /// <param name="item">
        /// The object to insert.
        /// </param>
        /// <exception cref="T:System.ArgumentOutOfRangeException">
        /// index is less than 0.-or-index is greater than <see cref="P:System.Collections.ObjectModel.Collection`1.Count"/>.
        /// </exception>
        protected override void InsertItem(int index, DateTimeFormatting item)
        {
            var existingItem = Items.Where(i => GetKeyForItem(i) == GetKeyForItem(item)).FirstOrDefault();
            if (existingItem != null)
            {
                SetItem(IndexOf(existingItem), item);
                return;
            }

            base.InsertItem(index, item);
        }

        /// <summary>
        /// Gets the <see cref="XmlTypeCode"/> of the formatting.
        /// </summary>
        /// <returns>
        /// The key for the specified element.
        /// </returns>
        /// <param name="item">The element from which to extract the key.</param>
        protected override XmlTypeCode GetKeyForItem(DateTimeFormatting item)
        {
            return item.DataType;
        }

        private void AddFormatting(XmlTypeCode typeCode, string format)
        {
            Add(
                    new DateTimeFormatting
                    {
                        DataType = typeCode,
                        Format = format
                    });
        }

        private void CreateDefaultCollection()
        {
            AddFormatting(XmlTypeCode.DateTime, "g");
            AddFormatting(XmlTypeCode.Date, "d");
            AddFormatting(XmlTypeCode.Time, "t");
            AddFormatting(XmlTypeCode.GYearMonth, "Y");
            AddFormatting(XmlTypeCode.GMonthDay, "m");
            AddFormatting(XmlTypeCode.GYear, "yyyy");
            AddFormatting(XmlTypeCode.GMonth, "MM");
            AddFormatting(XmlTypeCode.GDay, "dd");
        }
    }
}
